/**
 * 
 */
package com.onewingu.gcs.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * @author er66
 *
 */
@PersistenceCapable
public class SharedGroup {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String saGroupId;
	
	//map of saContactID -> sharedContact
	@NotPersistent
	private Map<String, SharedContact> contactMap;
	
	@Persistent
	private List<SharedContact> sharedContacts;
	
	//map of userId -> userGroupId
	@NotPersistent
	private Map<String, String> groupMap;
	
	@Persistent
	private List<String> sharedUserIds;
	
	@Persistent
	private List<String> sharedUserGroupIds;
	
	@NotPersistent
	private boolean deleteFlag;
	
	public SharedGroup(String saGroupId) {
		this.saGroupId = saGroupId;
		this.contactMap = new HashMap<String, SharedContact>();
		this.groupMap = new HashMap<String, String>();
		this.sharedContacts = new ArrayList<SharedContact>();
		this.sharedUserIds = new ArrayList<String>();
		this.sharedUserGroupIds = new ArrayList<String>();
	}
	
	public void postLoad() {
		this.contactMap = new HashMap<String, SharedContact>();
		this.groupMap = new HashMap<String, String>();

		for(SharedContact sharedContact : sharedContacts) {
			sharedContact.postLoad();
			contactMap.put(sharedContact.getSaContactID(), sharedContact);
		}
		
		for(int i = 0; i < sharedUserIds.size(); i++) {
			String sharedUserName = sharedUserIds.get(i);
			String sharedUserGroupId = sharedUserGroupIds.get(i);
			groupMap.put(sharedUserName, sharedUserGroupId);
		}
	}
	
	public void preSave() {
		for(SharedContact sharedContact : contactMap.values()) {
			sharedContact.preSave();
		}

		sharedContacts = new ArrayList<SharedContact>(contactMap.values());
		sharedUserIds = new ArrayList<String>(groupMap.keySet());
		sharedUserGroupIds = new ArrayList<String>(groupMap.values());
	}

	/**
	 * @return the saGroupId
	 */
	public String getSaGroupId() {
		return saGroupId;
	}

	/**
	 * @param saGroupId the saGroupId to set
	 */
	public void setSaGroupID(String saGroupId) {
		this.saGroupId = saGroupId;
	}

	/**
	 * @return the contactMap
	 */
	public Map<String, SharedContact> getContactMap() {
		return contactMap;
	}

	/**
	 * @param contactMap the contactMap to set
	 */
	public void setContactMap(Map<String, SharedContact> contactMap) {
		this.contactMap = contactMap;
	}

	/**
	 * @return the deleteFlag
	 */
	public boolean isDeleteFlag() {
		return deleteFlag;
	}

	/**
	 * @param deleteFlag the deleteFlag to set
	 */
	public void setDeleteFlag(boolean deleteFlag) {
		this.deleteFlag = deleteFlag;
	}

	/**
	 * @return the groupMap
	 */
	public Map<String, String> getGroupMap() {
		return groupMap;
	}

	/**
	 * @param groupMap the groupMap to set
	 */
	public void setGroupMap(Map<String, String> groupMap) {
		this.groupMap = groupMap;
	}
	
	
}
